//
//  JZGCarSaleImageCell.m
//  JingZhenGu
//
//  Created by Gaowl on 15/6/5.
//  Copyright (c) 2015年 Mars. All rights reserved.
//

#import "JZGCarSaleImageCell.h"
#import "JZGAvatarBrowser.h"


@interface JZGCarSaleImageCell ()

@property(nonatomic, weak) UIView *imageContainerView;

@property(nonatomic, weak) UIButton *addImageButton;

@property(nonatomic, strong) NSMutableArray *imageViewArray;
/**删除按钮*/
@property(nonatomic, weak) UIButton *deleteBtn;

@end

@implementation JZGCarSaleImageCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"carSaleImageCell";
    JZGCarSaleImageCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil)
    {
        cell = [[JZGCarSaleImageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
        [self makeCellSubviews];
    return self;
}

/**创建cell所有控件*/
- (void)makeCellSubviews
{
    UIView *imageContainerView = [[UIView alloc]init];
    [self addSubview:imageContainerView];
    self.imageContainerView = imageContainerView;
    
    UIButton *addImageButton = [[UIButton alloc]init];
    [addImageButton setBackgroundImage:[UIImage imageNamed:@"married_photo"] forState:UIControlStateNormal];
    [addImageButton addTarget:self action:@selector(addImageButtonDidClick) forControlEvents:UIControlEventTouchDown];
    self.addImageButton = addImageButton;
    [self.imageContainerView addSubview:addImageButton];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.addImageButton.frame = _cellFrame.addImageButtonFrame;
    self.imageContainerView.frame = _cellFrame.imageContainerViewFrame;
}
/**点击添加图片按钮*/
- (void)addImageButtonDidClick
{
    if ([self.delegate respondsToSelector:@selector(carSaleImageCellDidClickImageButton)]) {
        [self.delegate carSaleImageCellDidClickImageButton];
    }
    if ([self.delegate respondsToSelector:@selector(carSaleImageCellDidClickImageButtonAndCellTag:)]) {
        [self.delegate carSaleImageCellDidClickImageButtonAndCellTag:self.tag];
    }
}

-(void)setAllPhotoNumber:(NSInteger)allPhotoNumber
{
    _allPhotoNumber = allPhotoNumber;
}

- (void)setCellFrame:(JZGCarSaleImageCellFrame *)cellFrame
{
    _cellFrame = cellFrame;
    [self adjustImage];
}

- (void)adjustImage
{
    NSInteger index = 0;
    self.imageViewArray = nil;
    for (UIView *subview in self.imageContainerView.subviews) {
        if (![subview isEqual:self.addImageButton]) {
            [subview removeFromSuperview];
        }
    }
    
    for (id image in _cellFrame.cellModel.imageArray) {
        UIImageView *imageView =[[UIImageView alloc]init];
        if ([image isKindOfClass:[NSString class]]) {
            [imageView setImageWithURL:[NSURL URLWithString:image] placeholder:IMAGE(@"default")];
        }else if ([image isKindOfClass:[UIImage class]]) {
            [imageView setImage:image];
        }
        UITapGestureRecognizer *pressGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pressGestureImageView:)];
        [imageView addGestureRecognizer:pressGesture];
        
        imageView.userInteractionEnabled = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        NSValue *rectValue = (NSValue *)_cellFrame.imageViewsFrames[index];
        imageView.frame = [rectValue CGRectValue];
        [self.imageViewArray addObject:imageView];
        
        UIButton *deleteBt=[[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) - 22 - 4, CGRectGetMinY(imageView.frame) + 4, 22, 22)];
        [deleteBt addTarget:self action:@selector(deleteBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [deleteBt setBackgroundImage:[UIImage imageNamed:@"use_delete"] forState:UIControlStateNormal];
        deleteBt.imageView.contentMode = UIViewContentModeCenter;
//        deleteBt.center = CGPointMake(CGRectGetMaxX(imageView.frame), CGRectGetMinY(imageView.frame));
        deleteBt.tag=index + 999;
        self.deleteBtn=deleteBt;
        [self.imageContainerView addSubview:imageView];
        [self.imageContainerView addSubview:deleteBt];
        index++;
    }
    if (_cellFrame.cellModel.imageArray.count == _allPhotoNumber) {
        self.addImageButton.hidden = YES;
    }else {
        self.addImageButton.hidden = NO;
    }
}

- (void)pressGestureImageView:(UITapGestureRecognizer *)pressGesture
{
    CGPoint localPoint = [pressGesture locationInView:self.imageContainerView];
    NSInteger count = self.imageViewArray.count;
    for (int i = 0; i < count; i++)
    {
        UIImageView *imageView = self.imageViewArray[i];
        if(CGRectContainsPoint(imageView.frame, localPoint))
        {
            //开始选择图片
            [JZGAvatarBrowser showImage:imageView];
            break;
        }
    }
}


- (void)deleteBtnDidClick:(UIButton *)deleteBtn
{
    NSInteger index = deleteBtn.tag-999;
    if ([self.delegate respondsToSelector:@selector(carSaleImageCellDelegateDidDeleteImageWithIndex:)]) {
        [self.delegate carSaleImageCellDelegateDidDeleteImageWithIndex:index];
    }
    if ([self.delegate respondsToSelector:@selector(carSaleImageCellDelegateDidDeleteImageWithIndex:andCellTag:)]) {
        [self.delegate carSaleImageCellDelegateDidDeleteImageWithIndex:index andCellTag:self.tag];
    }
    [self.imageViewArray removeObjectAtIndex:index];
    [self removeDeleteBtn];
}

/**移除删除按钮*/
- (void)removeDeleteBtn
{
    [self.deleteBtn removeFromSuperview];
    self.deleteBtn = nil;
}

#pragma mark - layz loding
- (NSMutableArray *)imageViewArray
{
    if (!_imageViewArray) {
        _imageViewArray = [NSMutableArray array];
    }
    return _imageViewArray;
}

@end
